package com.slipper.system.message.controller;

import com.slipper.system.message.bo.MessageStatusBO;
import com.slipper.system.message.service.MessageStatusService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.message.bo.MessageBO;
import com.slipper.system.message.service.MessageService;
import com.slipper.constants.Constants;
import com.slipper.util.ResultUtil;
import com.slipper.util.Tool;

import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/message")
public class MessageController {
    @Resource
    private MessageService messageService;
    @Resource
    private MessageStatusService messageStatusService;
    
    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody MessageBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }

        MessageBO result = messageService.queryBy(bo.getId());
        MessageStatusBO qo = new MessageStatusBO();
        qo.setMessageId(result.getId());
        result.setReceiverIds(messageStatusService.queryList(qo).stream()
                .map(item -> item.getOrganizationId() + "@" + item.getEmployeeId())
                .collect(Collectors.joining(",")));
        return ResultUtil.successResult(result);
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody MessageBO bo) {
        return ResultUtil.successResult(messageService.queryList(bo));
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody MessageBO bo) {
        return ResultUtil.successResult(messageService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody MessageBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(messageService.save(bo), bo);
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody MessageBO bo) {
        return ResultUtil.deleteResult(messageService.delete(bo));
    }
}
